/**
 * Rename image files randomly
 * @authors: Bertrand GUILLAUMOND
 * @version 1.0
 */


import java.io.File;
import java.io.*;
import java.lang.*;

public class Rename{

	public static void main(String[] args)
	{
		Rename.renameFile(new File("."));
		System.exit(0);
	}

	public static void renameFile ( File directory ) {
		    
		    if ( directory.isDirectory ( ) ) {
		            File[] list = directory.listFiles();
		            if (list != null){
			            for ( int i = 0; i < list.length; i++) {
			                    Rename.renameFile( list[i]);
			            } 
		            } else {
		            	System.err.println(directory + " : Unable to open directory.");
		            }
		    }
			else
			{

				String ext = Rename.getFileExtension(directory.toString()); 
				if(ext.equals("jpg") ||ext.equals("JPG") || ext.equals("jpeg") || ext.equals("JPEG") || ext.equals("png") || ext.equals("PNG"))
				{
					File destination;
					boolean success = false;
					while(!success)
					{
						destination = new File("ILoveTran-"+Rename.generate(6)+'.'+ext);
						success = directory.renameTo(destination);
					}
				}
				
			}
	}

	public static String generate(int length)
	{
			String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
			String pass = "";
			for(int x=0;x<length;x++)
			{
			   int i = (int)Math.floor(Math.random() * 62);
			   pass += chars.charAt(i);
			}
			return pass;
	}

	public static String getFileExtension(String fileName) {
		File tempFile = new File(fileName);
		tempFile.getName();
		int posPoint = tempFile.getName().lastIndexOf('.');
		if (0 < posPoint && posPoint <= tempFile.getName().length() - 2 ) {
		    return tempFile.getName().substring(posPoint + 1);
		}    
		return "";
	}
}


